package com.situ.web.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.web.dao.ICourseDao;
import com.situ.web.entity.Course;
import com.situ.web.util.JDBCUtil;

public class CourseDaoImpl implements ICourseDao{
	
	@Override
	public List<Course> selectAll(){
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Course> list = new ArrayList<Course>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name`,`credit` FROM `course`";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id, name, credit);
				list.add(course);
			}
			for(Course course : list) {
				System.out.println(course);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;
	}


	@Override
	public int delete(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `course` WHERE `id`=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count:" + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		return count;
	}

	@Override
	public int insert(Course course) {
		int count = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO course(`id`,`name`,`credit`) VALUES(?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, course.getId());
			statement.setString(2, course.getName());
			statement.setInt(3, course.getCredit());
			count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count:" + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		return count;
	}

	@Override
	public List<Course> selectPage(int offset, int pageSize) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<Course> list = new ArrayList<Course>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT * FROM `course` LIMIT ?,?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, offset);
			statement.setInt(2, pageSize);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id, name, credit);
				list.add(course);
			}
			for (Course course : list) {
				System.out.println(course);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		
		return list;
	}

	@Override
	public int selectTotalCount() {
		int count = 0;
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT COUNT(*) as count FROM `course`";
			statement = connection.prepareStatement(sql);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			if(resultSet.next()) {
				count = resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return count;
	}


	public int update(Course course) {
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE course Set `name`=?,`credit`=? WHERE `id`=?";
			statement = connection.prepareStatement(sql);
			statement.setString(1, course.getName()); 
			statement.setInt(2, course.getCredit());
			statement.setInt(3, course.getId()); 
			count = statement.executeUpdate();
			System.out.println(statement);
			System.out.println("count:" + count);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		return count;
	}


	public Course selectId(int selectId) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Course course = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name`,`credit` FROM `course` WHERE `id`=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1,	selectId);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int credit = resultSet.getInt("credit");
				course = new Course(id, name, credit);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return course;
	}
}
